﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ArchSoft
{
    public partial class SearchListView : Form
    {
        string _projectPath;
        //ProjectManager _projectManager = null;

        T3VForm _parentForm = null;
        T3VForm _form = null;
        T3VListView _view = null;

        public void setPath(string path)
        {
            _projectPath = path;
        }

        public SearchListView()
        {
            InitializeComponent();
        }

        public void setForm(T3VForm form)
        {
            this._parentForm = form;
        }

        public void setList(T3VListView view)
        {
            this._view = view;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(_projectPath);

            try
            {

                string mau = "";
                //string mau = "*" + textBox1.Text + "*";
                string caption = _parentForm.CaptionWindow;
                if (caption.Trim() == "Xref")
                {
                     mau = "A_" + textBox1.Text + ".dwg";
                }
                else
                {
                    // mau = "?????_" + textBox1.Text + ".dwg";
                    mau = "*" + textBox1.Text + "*";
                 }
                
                string[] kq = Directory.GetFiles(_projectPath, mau, SearchOption.AllDirectories);

                //foreach (string name in kq)
                //{
                //    MessageBox.Show(Path.GetFileNameWithoutExtension(name));
                //}
                bool flag = false;
                string name1 = "";
                string name2 = "";
                if (caption.Trim() == "Xref")
                {
                    foreach (string name in kq)
                    {
                        name1 = Path.GetFileNameWithoutExtension(name).ToString();
                        //name2 = name1.Substring(6);
                        foreach (ListViewItem lvt in _parentForm.t3VListView1.Items)
                        {

                            if (lvt.Text.Trim() == name1.Trim())
                            {
                                // _parentForm.Focus();
                                //  lvt.Focused = true;
                                lvt.Selected = true;
                                flag = true;
                            }
                        }
                    }
                }
                else
                {
                    foreach (string name in kq)
                    {
                        // name1 = Path.GetFileNameWithoutExtension(name).ToString();
                        // name2 = name1.Substring(6);
                        //foreach (ListViewItem lvt in _parentForm.t3VListView1.Items)
                        //{
                        //    //string tag =  lvt.Tag.ToString();
                        //    if (lvt.Text.Trim() == name2.Trim())
                        //    {
                        //        // _parentForm.Focus();
                        //        //  lvt.Focused = true;
                        //        lvt.Selected = true;
                        //        flag = true;
                        //    }
                        //}

                        name1 = Path.GetFileNameWithoutExtension(name).ToString();
                        foreach (ListViewItem lvt in _parentForm.t3VListView1.Items)
                        {
                            
                            if (name1.Trim().IndexOf(lvt.Text.Trim()) >0 )
                            {
                                // _parentForm.Focus();
                                //  lvt.Focused = true;
                                lvt.Selected = true;
                                flag = true;
                            }
                        }
                    }
                }
                if (!flag)
                {
                    //MessageBox.Show("Drawing not found!");
                    MessageBox.Show("Bản vẽ không tìm thấy!");
                }

                this.Close();


              ////  _parentForm.Focus();

               // Search .ams

                //string dir = Directory.GetParent(_projectPath).ToString();
                
                //string ams = "*.ams";

                //string[] dir1 = Directory.GetFiles(dir, ams);
                //string filename = dir1[0];


              //  string[] windowName = Directory.GetDirectories(_projectPath);
                //foreach (string name in windowName)
                //{
                //    string  name1 = Path.GetFileName(name).ToString();
                //    _form = new T3VForm(_projectPath, name1);
                //     int count =  _form.t3VListView1.Items.Count;
                //     foreach (ListViewItem lvl in _form.t3VListView1.Items)
                //     {
                //         lvl.Selected = true;
                //     }
                //}
                //string name = Path.GetFileName(windowName[0]);
                //_form = new T3VForm(_projectPath, name);
                //foreach (ListViewItem lvl in _form.t3VListView1.Items)
                //{
                //    lvl.Selected = true;
                //}

                //_form.UpdateAllBroTitle();
                //_form.UpdateDrawingFiles();

                //foreach (ListViewItem lvl in _parentForm.t3VListView1.Items)
                //{
                //    lvl.Selected = true;
                //}



            }
            catch (Exception ex)
            {

            }
        }

        public string[] getfileName()
        {
            string mau = "*" + textBox1.Text + "*";

            string[] kq = Directory.GetFiles(_projectPath, mau, SearchOption.AllDirectories);

            //foreach (string name in kq)
            //{
            //    MessageBox.Show(Path.GetFileNameWithoutExtension(name));
            //}
            return kq;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}